[% setvar title Scope of Polymorphic References and Objects %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Scope of Polymorphic References and Objects</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Syloke Soong &lt;<a href='mailto:syloke.soong@nsc.com'>syloke.soong@nsc.com</a>&gt;
  Date: 15 Aug 2000
  Last Modified: 16 Aug 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 108
  Version: 2
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Syntax for Polymorping and Scoping References</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>This is a proposal to define the scoping and overloading of Perl
references.  The polymorphic signature is proposed to comprise the arglist
members, candidate operators and the return list members. In other words,
rather than the tradition of using just the head (java) or head+operator
(c++), the proposal is for head+tail or head+operator+tail polymorphic
signatures .</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Reference to RFC89 is recommended to relate to fixating or constraining
the data type of a variable.</p>
<p>For comprehension of this proposal, the following terms are held within
the bounds of this document. The concept stays, notwithstanding.</p>
<pre>  var $a;
  our $b;
  my $c;
  $d = 0;</pre>
<p>$a is defined to be referentiable as public.
$b is defined to be referenced within protected bounds of a package .
$c is defined to be referentiable only privately within a function.
$d is defined implicitly to be referentiable privately within a file.</p>
<a name='A new object module declaration method'></a><h2>A new object module declaration method</h2>
<p>Allow function arg list but continue to use @_;
The following would be equivalent forms in Perl5 and Perl6, respectively:</p>
<pre>  package Pine::Tree::State;
  sub genesis
  {
   my ($pkg, $oak, $maple) = @_;
   my $a = {};
   bless $a, $pkg;
   return $a;
  }

  package Pine::Tree::State;
  var genesis:(\new);
  function new ($oak :int, $maple: double)
  {
   bless $a:const={}, genesis;
   our $world={};

   return $a;
  }
  function aux ($spruce:int)
  {
   my $world = this-&gt;$world; # this is instance handle of self object

  # my $world = our-&gt;$world; # how about using our?
  # my $world = my-&gt;$world; # or what about my?
  }</pre>
<a name='Overloading of references'></a><h2>Overloading of references</h2>
<p>A reference may be overloaded with a search list, thus acting as a container.</p>
<pre>  my $a1:(int) = 3;
  my $a2 = 4;
  my $a3:(int);
  my $a:($a1, $a2, $a3, double);</pre>
<p>$a is constrained in the following precedence
- references $a1 if an integer evaluation is done
- references $a2 if a liberal evaluation is done
- references double if a double evaluation is done
- $a3 is unreachable because $a1 is precedent in the search list</p>
<pre>  $a = 11; #equivalent to $a1 = 11;
  $b:(int) = $a ; #equivalent to int $b = $a1;
  $a = 11.1; $c:(double) = $a ; #equivalent to double $c = 11.1;
  $a = 11.1; $c:(varchar) = $a ; #equivalent to varchar $c = '11.1';
  $a = 11.9; $d:(int) = $a ; #equivalent to int $c = 11;
  $a = 11.9 + 0.5 ; $ceiling:(int) = $a ; #equivalent to int $ceiling = 12;</pre>
<a name='Overloading of object containers'></a><h2>Overloading of object containers</h2>
<pre>  package Pine::Tree::State;

  var genesis:(\new_a, \new_b, \new_c); 
  var habakuk:(\new_b, \new_c);
  var getab:(\new_a, \new_b);

  function new_a ($oak int, $maple double)
  {
   return bless $a:const={}, genesis;
  }

  function new_b ($oak:(varchar,int), $maple)
  {
   bless $a={},genesis;
   bless $b={},habakuk;
   my $c={};
   var $r:($a,$b,$c); 

   return $r;
  }

  function new_c (varchar $oak, $maple, $pine)
  {
   bless $a:const={}, (genesis,habakuk); 
   return $a
  }

  #!/usr/bin/perl
  use Pine::Tree::State;

  my $a:(varchar,int);
  my habakuk $pineapple = genesis Pine::Tree::State($a, 'Banana');
  my $melon = genesis Pine::Tree::State($a, 'Banana');</pre>
<p>Blessing genesis confers it a constructor container. genesis is overloaded with
two function references stored in an array. Refer to RFC89 for fixating the
data type of a variable. getab is an overloaded function container but not of
constructors.  new_c acts as a constructor through genesis or habakuk but not
through getab.</p>
<p>Matching a call to a member of a container is by means of a signature due to
the combination of the function argument list and return list. RFC89
suitability and precendent rules apply to matching signatures. The order of the
array dictates the search order to match a call to the members of an overloaded
reference.</p>
<p>habakuk is an alternate constructor within the package.</p>
<p>new_b is an insteresting constructor/function. Can you theorise what happens
when new_b is activated through constructor container genesis, or constructor
container habakuk, or object container getab?</p>
<p>Notice new_b has arglist member $oak:(varchar,int), while new_c has varchar
$oak.  Effects of type casting and contraint are propagated into the function
body.</p>
<p>Signature of new_b is invoked by the genesis of Pine::Tree::State by
$pineapple. $new_b-&gt;$r is an overloaded reference. Return of $r is a
polymorphic return. $pineapple invokes new_b through genesis but has a casted
reception for habakuk. So even though it is genesis'ed it becomes a habakuk
object. Meanwhile $melon becomes a genesis object by having the value of
new_b-&gt;$r:$c returned to it. The liberal new_b-&gt;$r:$c is presented when a
programmer desires to have a default return signature match.</p>
<p>Presence of an arglist does not preclude the referentiability of @_; @_ allows
variable number of arguments into a function.</p>
<a name='A new object module component exportability'></a><h2>A new object module component exportability</h2>
<pre>  package Pine::Tree::State;

  var $A;
  our $B;
  my $C;
  $D = 0;
  var getx = \get_x;
  our gety = \get_y;
  my getz = \get_z;

  var new : (\new_a, \new_b);
  our $a={};

  function new_a ($oak:int)
  {
   bless $a, new;

   return $a
  }

  function new_b ($oak:int, $maple: double)
  {
   bless $a, new; 
   var getz1 = \get_z1;
   our getz2 = \get_z2;
   my getz3 = \get_z3;

   function $get_a1 = {return 'a1'};
   function $get_a2 = {return 'a2'};
   function $get_b = {return 'b'};
   function $get_c = {return 'c'};

   var geta = ($get_a,$get_a2);
   our getb = $get_b;
   my getc = $get_c;

   var $h;
   our $i;
   my $j;
   $k = 0;

   return $a;
  }

  function new_b ($oak :int)
  {
   bless $a={}, new;

   return $a
  }

  function get_x{ my ($a,$b) = @_;}
  function get_y{ my ($a,$b) = @_;}
  function get_z{ my ($a,$b) = @_;}
  function get_z1{ my ($a,$b) = @_;}
  function get_z2{ my ($a,$b) = @_;}
  function get_z3{ my ($a,$b) = @_;}</pre>
<p>All functions are not accessible outside the file except that, get_x,
get_z1, $get_a1, $get_a2, $h, $A are implicitly exported and referentiable
by as</p>
<pre>  use Pine::Tree::State;
  getx($a1,$b1);
  my $p1 = new Pine::Tree::State ($oakhurst, $maplest);
  $p1-&gt;getz1($a1,$b1);
  $p1-&gt;geta($a1,$b1);
  $p1-&gt;i = 0;
  $A = 1;</pre>
<p>get_y, get_z2, $get_b, $i, $B are implicitly protected to be used and
referentiable only within the package as,</p>
<pre>  package Pine::Spruce::City;
  use Pine::Tree::State;
  function pining
  {
   gety($a1,$b1);
   my $p2 = new Pine::Tree::State ($oakhurst, $maplest);
   $p2-&gt;getz2($a1,$b1);
   $p2-&gt;getb($a1,$b1);
   $p2-&gt;j = 0;
   $B = 1;
  }</pre>
<p>get_z3, $get_c, $j are implicitly private and referentiable only within the
function new_b as,</p>
<pre>  getz3($a1,$b1);
  getc($a1,$b1);
  $j = 0;</pre>
<p>$C, $D and all the following functions are protected to be used within the file
as,</p>
<pre>  $C = 0;
  $D = 1;
  getz($a,$b);
  get_x($a,$b);
  get_y($a,$b);
  get_z($a,$b);
  get_z1($a,$b);
  get_z2($a,$b);
  get_z3($a,$b);</pre>
<p>Use and not using of &quot;my&quot; outside any function to declare a reference
within a file is indistinguishable as in the case of $C and $D.</p>
<p>All functions defined inside function new_b is accessible only within new_b
using their original names.</p>
<a name='Head + Tail polymorphic signature'></a><h2>Head + Tail polymorphic signature</h2>
<p>An overloaded function list need not contain only references to functions but
to variables and lists as well. Matching a call to a member of an overloaded container is
by means of matching the signature comprising both the function argument list
and return list.</p>
<pre>  function $a1($x: int) = {};
  function $a2($x: int) = {my $k:double; return $k };
  function $a3($x:int) = {my @k; return \@k };
  my $b;
  my @c;
  my %d;
  function $a4($x:int) = {my $e:(int,\@,\%); return $e};

  var abcd:($a1, $b, \@c, \%d, $a2, $a3,$e);</pre>
<p>The calls to the overloaded container abcd
abcd(my $i:int = 3); #matches $a1
abcd(my $i:int = 3) double; #matches $a2
abcd(my $i = 'a') ; #matches $b
abcd(my $i = [] ) ; #matches \@c
abcd(my $i = {} ) ; #matches \%d</p>
<p>Traditional overload matching using arglist alone has been unsatisfactory
especially in needing to dictate the match using the LHS:</p>
<pre>  my $j2 = abcd(my $i: int); #matches $a1
  my $j2:double =  abcd(my $i: int); #matches $a2
  my $j3:\@ = abcd(my $i :int); #matches $a3
  my $j4:(int,\@,\%) = abcd(my $i: int); #matches $a4</pre>
<p>To avoid complexity, matching involving arrays and hashes should not descend
beyond the first level.</p>
<p>If an array is fixed as an array of ints or double:</p>
<pre>  function k{ my $k:(double) = []; return $k;}
  my @k1:(int) = k(); #no match, returns undef
  my @k2:(double) = k (); #matches function k</pre>
<p>However if an array is fixed as an array of array, the second array level
is not investigated for matches.</p>
<pre>  function m1{ my $k:(\@,double) = []; return $k;}
  function m2
  { my $n1 = [a:(int), b:(double)];
    my $n:($n1,double) = [];
    return $n;
  }
  my m:(\m1,\m2);

  my $k1 = [a:(int), b:(double)];
  my @k:($k1,double) = m ();
  #matches function m1 because m1 is first in the list</pre>
<p>m2 will be unreachable through container m() because m1 and m2 have similar
function signatures, despite the fact that $n1 and $k1 match would dictate that
m2() should be the correct signature match. But a match due to $n1 and $k1
would require descending the arrays referenced by $k1 and $k2. Descending
arrays to match signatures would make it utterly impossible to avoid divergent
complexities. Though it might be good for theoretical studies.</p>
<a name='Head:operator:tail polymorphic signature'></a><h2>Head:operator:tail polymorphic signature</h2>
<pre>  package Pine::Tree::State;
  var popeye:(\new_a, \new_b); #Polymorphic head constructor container
  operator popeye:(\func_a1, \func_a2, \func_b, \func_c, \func_d);

  function new_a ($oak :int, $maple: double)
  { return bless $a:const={}, popeye}

  function new_b ($oak :varchar, $maple)
  { return bless $a:const={}, popeye}

  #Polymorphic head for any operators
  #Polymorphic signature is (int,double,const):liberal:liberal
  function func_a1 ($a:int, $b:double, int $c:const)
  {return my $abc} 

  #Polymorphic head for any operators
  #Polysig is (int,double,const):liberal:int
  function func_a2 ($a:int, $b:double, int $c:const)
  {return my $abc:int}

  #Polymorphic head for +,- operators
  #Polysig is (int,double,const):(+,-):(int,varchar)
  function func_b ($a:int, $b:double, int $c:const):(+,-)
  {
   my $operators = @OPERATORS; # Const Array referencing const values of operators (_,-) 
   my $operator_used = $OPERATOR; # Const value of operator invoked
   if ($operator_used == '+')
  	{ return my $abc:int}
   else { return my $abc:varchar}
  }

  #Polymorphic head for *,- operators
  #Polysig is (int,double,const):(*,/):double
  function func_c ($a:int, $b:double, int $c:const):(*,/)
  {return my $abc:double}

  #Polymorphic head for =,&gt;, \qwertyuiop operators
  #Polysig is (int,double,const):(=,&gt;,\qwertyuiop):double
  function func_d ($a:int, $b:double, int $c:const):(=,\qwertyuiop)
  {return my $abc:double}

  use Pine::Tree::State;
  #Normal equal operator in action during object construction:
  my popeye $lemon = popeye ($o:int, $m: double);
  my popeye $lime = popeye ($o:varchar, $m);

  #Redefined equal and plus operator actions:
  $lime = $lemon + 5;

  #Programmer defined operator in action:
  sub dofirst{} sub dosecond{}
  $lemon qwertyuiop $lime ? dofirst(): dosecond();</pre>
<p>The problem with tail signature is when return returns a function which returns
another function which returns another function ....  The return signature
should be determined only by the return statements within the constructor,
otherwise it's presumed liberal and inconsequential.</p>
<a name='Scope of return reference does not affect scope of returned object;'></a><h2>Scope of return reference does not affect scope of returned object;</h2>
<pre>  package Pine::Tree::State;
  var genesis:(\new_a, \new_b, \new_c);

  function new_a ($oak :int, $maple: double)
  {
   var $a={};
   bless $a, genesis;
   return $a;
  }

  function new_b ($oak varchar, $maple)
  {
   our $a={};
   bless $a, (genesis,habakuk); 
   return $a;
  }

  function new_c ($oak, $maple, $pine)
  {
   our $a;
   bless $a, (genesis,habakuk); 
   return $a
  }

  #!/usr/bin/perl
  use Pine::Tree::State;
  our $newb = \new_b();
  bless getabc : (\new_a, $newb, \new_c);</pre>
<p>The use of var, our or my should be inconsequential to the accessibilities of
new_a, new_b or new_c since the refences are returned anyway. However - var $a,
our $a and my $a become globally, package and function accessible respectively
and subjected to interference as bounded by their scopes. new_a has its own
globally accessible $a, while package $a is shared between new_b and new_c.</p>
<p>A call to getabc from anywhere except packages with root Pine will implicitly
preclude new_b() from the search list due to: our $newb;</p>
<a name='Instance references and shared references'></a><h2>Instance references and shared references</h2>
<pre>  package Math::Complex;
  var (re,im):(int,double); #globally accessible objects
  var $gcounter; #globally shared reference
  our $pcounter; #package shared reference

  #globally accessible container complex singly loaded with a constructor 
  var complex:(\comp);

  #Polysig is (re,im):liberal:complex
  function comp (my $re:re, my $im:im)
  {
  # our references declared inside constructor function is shared within package
  # but only through the instance handle of the object
   our ($rev,$imv):(\re,\im);
   my $a={}; #instance hash
   return bless $a, complex; #return instance handle
  }

  var plus:(\plus0,\0plus1,\plus2); #overloaded container with three candidate functions
  var mult:(\mult0,\mult1,\mult2); #overloaded container with three candidate functions

  #polysig complex:liberal:complex
  function plus0 (my $u:complex)
  {
   my $re:re = this-&gt;plus($u); #looking for polysig complex:liberal:re
   my $im:im = this-&gt;plus($u);#looking for polysig complex:liberal:im
   my $ret:\complex = \this;
   $ret-&gt;$rev = $re;
   $ret-&gt;$imv = $im;
   return $ret; 
  }

  #polysig complex:liberal:re
  function plus1 (my $u:complex)
  {
   #add $ref of this instance with $ref of instance $u
   return  my $re:re = this-&gt;$rev + $u-&gt;$rev;  # this is instance handle of self
  }

  #polysig complex:liberal:im
  function plus2 (my $u:complex)
  {
   #similarly
   return  my $im:im = this-&gt;$imv + $u-&gt;$imv;
  }

  #polysig complex:liberal:complex
  function mult0 (my $u:complex)
  {
   my $re:re = this-&gt;mult($u); #looking for polysig complex:liberal:re
   my $im:im = this-&gt;mult($u);#looking for polysig complex:liberal:im
   my $ret:\complex = \this;
   $ret-&gt;$rev = $re;
   $ret-&gt;$imv = $im;
   return $ret; 
  }

  #polysig complex:liberal:re
  function mult1 (my $u:complex)
  {
   return my $re:re = this-&gt;$rev * $u-&gt;$rev - this-&gt;$imv * $u-&gt;$imv
  }

  #polysig complex:liberal:im
  function mult1 (my $u:complex)
  {
   return my $im:im = this-&gt;$rev * $u-&gt;$imv + this-&gt;$imv * $u-&gt;$rev;
  }</pre>
<a name='Complex numbers using head+tail polysignature'></a><h2>Complex numbers using head+tail polysignature</h2>
<p>Using package Math::Complex defined previously,</p>
<pre>  #!/usr/bin/perl
  use Math::Complex;
  my $M = complex Math::Complex(3,5.5);
  my $N = complex Math::Complex(7.5,2);
  my $C2:complex =  $N-&gt;plus ($M) ;#looking for polysig complex:liberal:complex
  my $C1:complex =  $M-&gt;mult ($N) ;#looking for polysig complex:liberal:complex</pre>
<a name='Complex numbers using head:operator:tail polysignature'></a><h2>Complex numbers using head:operator:tail polysignature</h2>
<pre>  #overloaded operator container with four candidate functions
  operator complex:(\replus,\implus,\remult,\immult);
  or
  #container hierarchically overloaded with four candidates
  operator complex:(\plus,\mult) ;
  var plus:(\replus,\implus); 
  var mult:(\remult,\immult); 

  #polysig complex:(+,-):complex
  function plus (my $u:complex):(+,-)
  {
   my $re:re = this $OPERATOR $u; #looking for polysig complex:(+,-):re;
   my $im:im = this $OPERATOR $u; #looking for polysig complex:(+,-):im;

   my $ret:\complex = \this;
   $ret-&gt;$rev = $re;
   $ret-&gt;$imv = $im;
   return $ret; 
  }

  #polysig complex:(+,-):re
  function replus (my $u:complex):(+,-)
  {
   return  my $re:re
    = $$OPERATOR=='+'
    ? this-&gt;$rev + $u-&gt;$rev
    : this-&gt;$rev - $u-&gt;$rev
    ;
  }

  #polysig complex:(+,-):im
  function implus (my $u:complex):(+,-)
  {
   return  my $im:im 
    = $$OPERATOR=='+'
    ? this-&gt;$imv + $u-&gt;$imv
    : this-&gt;$imv - $u-&gt;$imv
    ;
  }

  #polysig complex:(*,/):complex
  function mult0 (my $u:complex)
  {
   my $re:re = this $OPERATOR $u; #matching polysig complex:(*,/):re
   my $im:im = this $OPERATOR $u; #matching polysig complex:(*,/):im
   my $ret:\complex = \this;
   $ret-&gt;$rev = $re;
   $ret-&gt;$imv = $im;
   return $ret; 
  }

  #polysig complex:(*,/):re
  function remult (my $u:complex)
  {
   return  my $re:re
    = $$OPERATOR=='*'
    ? this-&gt;$rev * $u-&gt;$rev
    : this-&gt;$rev / $u-&gt;$rev
    ;
  }

  #polysig complex:(*,/):im
  function immult (my $u:complex)
  {
   return  my $im:im 
    = $$OPERATOR=='*'
    ? this-&gt;$imv * $u-&gt;$imv
    : this-&gt;$imv / $u-&gt;$imv
    ;
  }


  #!/usr/bin/perl
  use package Math::Complex;
  my $M = complex Math::Complex(3,5.5);
  my $N = complex Math::Complex(7.5,2);
  my $C2:complex =  $N+$M ;#matching polysig complex:(+,-):complex
  my $C1:complex =  $M/$N ;#matching polysig complex:(*,/):complex</pre>
<a name='Overloading to provide default null instance'></a><h2>Overloading to provide default null instance</h2>
<p>The feature of mixed overloading may be used to provide the default null reference.</p>
<pre>  var complex:(\comp, our $compnull:const=undef);

  #Polysig is (re,im):liberal:complex
  function comp (my $re:re, my $im:im)
  {
   our ($rev,$imv):(\re,\im);
   my $a={}; #instance hash
   return bless $a, complex; #return instance handle
  }</pre>
<p>The above is similar to having, but without coding, the function compnul:</p>
<pre>  var complex:(\comp, our $compnull:const=\compnul);
  #Polysig is liberal:liberal:liberal
  function compnul
  {return undef
  }</pre>
<p>The polysig liberal:liberal:liberal can be used to match any case and therefore can be used as the final default case by placing it as the last member of the overloaded list.</p>
<a name='Finally'></a><h2>Finally</h2>
<p>There's no business like Perl business.</p>
<p>Is your polysig republican:liberal:democrat or reform:liberal:green ?</p>
<p>A pardon for the author should be in order for having wildly introduced
keywords and phrases. Full and enthusiatic support will be given to alternative
keywords, phrases and any rearrangements that promote symmetric and concentric
Perl. Another pardon should also be for having authored this RFC first but
published later than RFC95.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 28: Perl should stay Perl.</p>
<p>RFC 89: Controllable Data Typing.</p>
</div>
